<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<span style="font-size:18px;">
<html >
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="extinct/css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="extinct/css/commonly-used-show.css" />
    <script type="text/javascript" src="extinct/js/jquery.min.js"></script>
    <script type="text/javascript" src="extinct/js/bootstrap.js" ></script>
    <script type="text/javascript" src="extinct/js/jquery.cookie.js"></script>

    <title>土地管理</title>

</head>
<body class="mybody">

<jsp:include page="theTopMenuAdmin.jsp" />


<div class="container-fluid">
    <div class="row">


        <div class="col-md-12">
            <div class="col-md-12">
                    <div class="panel panel-primary">
                        <div class="panel-body">
                            <div class="alert alert-success">



                                <div class="container">
        <h3 style="text-align: center">土地信息</h3>
        <hr style="filter: alpha(opacity=100,finishopacity=0,style=3)" width="100%" color="#6f5999" size="3" />

        <div id="reset_form" class="container">
            <div class="form-group same-line">
                <label ><b style="color: red">*&nbsp</b>土地名称：</label>
                <input type="text" id="land_name" class="form-control edit" placeholder="请输入土地名称" maxlength="20">
            </div>

            <div class="form-group same-line-left">
                <label ><b style="color: red">*&nbsp</b>土地面积：(平方米)</label>
                <input type="number" id="land_area" class="form-control edit" placeholder="请输入土地面积" maxlength="10">
            </div>

            <div class="form-group same-line-left">
                <label ><b style="color: red">*&nbsp</b>耕种状态：</label>
                <select class="form-control edit" title="请选择" id="land_cultivation_status" >
                    <option value="" disabled selected hidden>请选择</option>
                    <option value="1">未耕种</option>
                    <option value="2">已耕种</option>
                    <option value="3">已耕出租</option>
                    <option value="4">未知</option>
                </select>
            </div>

            <div class="form-group same-line">
                 <label ><b style="color: red">*&nbsp</b>土地地址：</label>
                <input type="text" id="land_address" class="form-control edit" placeholder="请输入土地地址" maxlength="30">
            </div>

            <div class="form-group same-line-left">
                <label ><b style="color: red">*&nbsp</b>土地价格：(元)</label>
                <input type="number" id="land_price" class="form-control edit" placeholder="请输入土地价格" maxlength="10">
            </div>

            <div class="form-group same-line-left">
                 <label ><b style="color: red">*&nbsp</b>土地联系人：</label>
                <input type="text" id="land_contact" class="form-control edit" placeholder="请输入土地联系人" maxlength="10">
            </div>

            <div class="form-group same-line">
                 <label ><b style="color: red">*&nbsp</b>联系人电话：</label>
                <input type="number" id="land_contact_phone" class="form-control edit" placeholder="请输入联系人电话" maxlength="12">
            </div>

            <div class="form-group same-line-left">
                 <label ><b style="color: red">*&nbsp</b>土地图片：</label>
                <div>
                     <img style="float: left" id="head_port" src="purchase/img/head_portrait.png" width="90px" height="90px" >
                     <input type="file" onChange="selectImage(this)" style="width: 190px; float: right; margin-top: 30px; margin-left: 10px;">
                </div>
            </div>


        </div>

        <div style="margin-top: 30px;" class="form-group text-center same-line-clear">
            <input id="update_land" type="button" value="添加" class="btn btn-primary" >
            <input type="reset" value="重置" class="btn btn-danger interval" onclick="reset()">
        </div>

        <div class="text-center">
            <span id="user_messge" style="font-size:10px; color: red;"></span>
        </div>

</div>



                            </div>
                        </div>
                    </div>
                </div>
        </div>



    </div>

 </div>




<script type="text/javascript">
    var userdt = JSON.parse(sessionStorage.getItem("user"));
    if (userdt.data.userName != null){
        $("#usname").text(userdt.data.userName);
    }else {
        $("#usname").text(userdt.data.userLoginName);
    }

    //重置文本框
    function reset(){
        var addUser = document.getElementById("reset_form");
        var ipts = addUser.getElementsByTagName("input");
        for (var i = 0; i < ipts.length; i++){
            ipts[i].value = "";
        }
    }

    if ("1" !== "${param.id}"){
        landInfo();

        $("#update_land").attr("value","确认修改");
        $("#update_land").click(function(){
            updateLandInfo();
        });
    }else {
        $("#update_land").click(function(){
            addLand();
        });
    }

    var graphic;
    var imgurl = "";

    //添加土地
    function addLand(){
        var landName = $("#land_name").val();
        var landArea = $("#land_area").val();
        var landCultivationStatus = $("#land_cultivation_status").val();

        var landAddress = $("#land_address").val();
        var landPrice = $("#land_price").val();
        var landContact = $("#land_contact").val();
        var landContactPhone = $("#land_contact_phone").val();


        if ($.isEmptyObject(landName)){
            $("#user_messge").text("土地名称不能为空");
            return;
        }else if ($.isEmptyObject(landArea)){
            $("#user_messge").text("土地面积不能为空");
            return;
        }else if ($.isEmptyObject(landCultivationStatus)){
            $("#user_messge").text("耕种状态不能为空");
            return;
        }else if ($.isEmptyObject(landAddress)){
            $("#user_messge").text("土地地址不能为空");
            return;
        }else if ($.isEmptyObject(landPrice)){
            $("#user_messge").text("土地价格不正确");
            return;
        }else if ($.isEmptyObject(landContact)){
            $("#user_messge").text("土地联系人不正确");
            return;
        }else if ($.isEmptyObject(landContactPhone)){
            $("#user_messge").text("联系人电话不正确");
            return;
        }else {
            $("#user_messge").text("");
        }



        var postUrl = "${pageContext.request.contextPath}/member/addLand";
        var str = {
            "landownerId": userdt.data.id,
            "landName": landName,
            "landArea": landArea,
            "landCultivationStatus": landCultivationStatus,
            "landAddress": landAddress,
            "landPrice": landPrice,
            "landContact": landContact,
            "landContactPhone": landContactPhone
        };

        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(result){
                if (result.code === 502){
                    $("#user_messge").text(result.data);
                }else {
                    location.href = "${pageContext.request.contextPath}/landManagement";
                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });
    }


    function updateLandInfo(){
        var postUrl = "${pageContext.request.contextPath}/member/updateLand";

        var str = {
            "id": "${param.id}",
            "landName" : $("#land_name").val(),
            "landArea" : $("#land_area").val(),
            "landCultivationStatus" : $("#land_cultivation_status").val(),
            "landAddress" : $("#land_address").val(),
            "landPrice" : $("#land_price").val(),
            "landContact" : $("#land_contact").val(),
            "landContactPhone" : $("#land_contact_phone").val()
        };

        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(result){
                if (result.code === 502){
                    $("#user_messge").text(result.data);
                }else {
                    location.reload();
                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });
    }

    function landInfo(){
        var postUrl = "${pageContext.request.contextPath}/member/queryLand";

        var str = {
            "id": "${param.id}"
        };

        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(result){
                if (result.code === 502){
                    $("#user_messge").text(result.data);
                }else {
                    $("#land_name").val(result.data.landName);
                    $("#land_area").val(result.data.landArea);
                    $("#land_cultivation_status").val(result.data.landCultivationStatus);
                    $("#land_address").val(result.data.landAddress);
                    $("#land_price").val(result.data.landPrice);
                    $("#land_contact").val(result.data.landContact);
                    $("#land_contact_phone").val(result.data.landContactPhone);

                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });

    }

    function selectImage(file) {
        if (!file.files || !file.files[0]) {
            return;
        }
        var reader = new FileReader();
        reader.onload = function (evt) {
            document.getElementById("head_port").src = evt.target.result;
        }
        //通过文件流将文件转换成Base64字符串
        reader.readAsDataURL(file.files[0]);
        graphic = file.files[0];
    }

</script>

</body>
</html>
</span>